<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.mobile" placeholder="手机号" class="filter-item search-input" clearable />
      <el-input v-model="listQuery.companyName" placeholder="分公司名关键字" class="filter-item search-input" clearable />
      <el-select
        v-model="listQuery.city"
        placeholder="城市"
        clearable
        style="width: 120px"
        class="filter-item search-input"
      >
        <el-option v-for="item in cityOptions" :key="item.value" :label="item.label.trim()" :value="item.value" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <!--      <el-but ton class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">-->
      <!--        导出-->
      <!--      </el-button>-->
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.ID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分公司名" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span style="font-weight :bold;">{{ row.companyName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="城市" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span style="font-weight :bold;">{{ getCityLabel(row.city) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="{row}">
          <el-button size="small" type="primary" icon="el-icon-edit" @click="handleUpdate(row)">编辑</el-button>
          <!--        <el-button size="small" type="danger" plain>删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!--  编辑页  -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" fullscreen>
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="saveData"
        label-position="left"
        label-width="auto"
        style="width: 100%; margin-left:10px;"
      >
        <el-form-item label="分公司名" prop="companyName">
          <el-input v-model="saveData.companyName" placeholder="请输入分公司名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="saveData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="saveData.passWord" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="所在城市" prop="city">
          <el-select v-model="saveData.city" class="filter-item" placeholder="请选择城市">
            <el-option
              v-for="item in cityOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="listQuery.page"-->
    <!--      :limit.sync="listQuery.limit"-->
    <!--      @pagination="getList"-->
    <!--    />-->
  </div>
</template>

<script>
import { list, get, update } from '@/api/company'
import { listCity } from '@/api/selectOptions'
import { getLabelByValue, toSelectOptions } from '@/utils/common'
// import Pagination from '@/components/Pagination'

export default {
  components: {
    // Pagination
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        city: undefined,
        companyName: undefined,
        mobile: undefined
        // csv: undefined // 为1时，导出
      },
      cityOptions: undefined,

      // 以下为编辑页属性
      dialogStatus: '',
      dialogFormVisible: false,
      saveData: {
        companyName: '',
        mobile: '',
        passWord: '',
        city: ''
      },
      resetSaveData() {
        this.saveData = {
          companyName: '',
          mobile: '',
          passWord: '',
          city: ''
        }
      },
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        companyName: [{ required: true, message: '分公司名不能为空', trigger: 'change' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'change' }],
        passWord: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        city: [{ required: true, message: '所在城市不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getCityOptions()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data
        this.total = list.length
        this.listLoading = false
      })
    },
    getCityLabel(value) {
      return getLabelByValue(this.cityOptions, value)
    },
    getCityOptions() {
      listCity().then(response => {
        this.cityOptions = toSelectOptions(response.data)
      })
    },
    handleCreate() {
      this.resetSaveData()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // createArticle(this.saveData).then(() => {
          //   this.list.unshift(this.saveData)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: '成功',
          //     message: '创建成功',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      get({ ID: row.ID }).then(response => {
        this.saveData = Object.assign({}, response.data) // copy obj
        console.log(this.saveData)
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          update(this.saveData).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.submitDisabled = false
            this.getList()
          })
        } else {
          this.submitDisabled = false
        }
      })
    },
    handleDelete(row, index) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }
    // handleExport() {
    //   list(Object.assign(this.listQuery, { csv: 1 }), 'arraybuffer').then(response => {
    //     csvExport(response)
    //     this.listQuery.csv = 0
    //   })
    // }
  }
}
</script>

<style>
.search-input {
  width: 150px;
  margin-right: 15px;
}
</style>
